package java_1_24.leetcode;

import java.util.Stack;

/**
 * 用栈实现队列
 * 双栈模拟队列
 */
public class Num232_MyQueue {
    //实际存储元素的栈
    private Stack<Integer> s1=new Stack<>();
    //辅助栈
    private Stack<Integer> s2=new Stack<>();

    public Num232_MyQueue() {
    }
    public void push(int x){
        if(s1.isEmpty()){
            s1.push(x);
        }else{
            /**
             * 先把s1所有元素弹出放在s2
             */
            while(!s1.isEmpty()){
               // s2.push(s1.pop());
                int val=s1.pop();
                s2.push(val);
            }
            //将新元素直接放入s1,此时新元素就在s1栈底
            s1.push(x);
            //再将s2所有元素依次弹回s1
            while (!s2.isEmpty()){
                s1.push(s2.pop());
            }
        }
    }
    public int pop(){
        return s1.pop();
    }
    public int peek(){
        return s1.peek();
    }
    public boolean empty(){//注意leetcode给的是empty
        return s1.isEmpty();
    }
}
